﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using SDT.ServiceSystem;
using System.Threading;
using SDT.Libraries.Utilities;
using SDT.DataService.Services;
using SDT.DataService;


namespace SDT.CrawlSystem.MediaSrv
{
    public sealed partial class MDWorkUnit : WorkUnit
    {
        
        private static FileStream CreateFile(Uri uriName, int _cid, int _pageId, string date, ref string pFile)
        {
            FileStream localFile = null;
            string localSavePath;
            string fileName;
            FTC_BDSDataContext eDataContext = new FTC_BDSDataContext(); // Created by Vietlv
            SystemSpiderDataContext spiderDataContext = new SystemSpiderDataContext();
            try
            {
                string[] uriSegments = uriName.Segments;

                string pathDirectory = Global.Instance.DataFolder;
                //localSavePath = string.IsNullOrEmpty(pathDirectory) ? @"D:\WEB CRAWLER\Data\Image" + "\\" + _cid.ToString() + "\\" + _pageId.ToString() + "\\" : pathDirectory 
                //    + @"\" + _cid.ToString() + @"\" + date + @"\";

                localSavePath = string.IsNullOrEmpty(pathDirectory) ? @"C:\WEB CRAWLER\Data\Images\" : pathDirectory;


                if (uriSegments.Length > 1)
                {
                    if (!Directory.Exists(localSavePath))
                        Directory.CreateDirectory(localSavePath);
                                       

                    if (GenRandomNameImage(uriName.OriginalString) == string.Empty)
                        fileName = uriSegments[uriSegments.Length - 1];
                    else fileName = GenRandomNameImage(uriName.OriginalString);

                }
                else
                {
                    fileName = "default.html";
                }
                var currentSpiderLinkDownloadID = spiderDataContext.Spider_LinkDownloads.Where(e => e.DowloadID == _pageId).Select(e => e.PageID).SingleOrDefault();
               var currentArticles = eDataContext.Ads.Where(e => e.PageID == currentSpiderLinkDownloadID).SingleOrDefault();

                // Open the file to write to
                string saveFileName = localSavePath +currentArticles.ID + "_" + fileName;
                
                localFile = File.Open(
                    saveFileName,
                    System.IO.FileMode.Create,
                    System.IO.FileAccess.Write,
                    System.IO.FileShare.None
                    );

                //pFile = Global.Instance.LocalHost + @"/" + _cid.ToString() + @"/" + date +  @"/"
                // + fileName.ToString().ToUpper();
                //pFile = @"/Images/" + fileName.ToString().ToUpper();
            // Modifiel by Vietlv
                pFile = fileName.ToString().ToUpper();


            }
            catch (Exception ex)
            {
                if (localFile != null)
                    localFile.Close();

                pFile = string.Empty;
            }


            return localFile;
        }

        private static string GetFileServer(string _url, int _cid, int _pageId, string date)
        {
            string pFileServer;
            string fileName;
            Uri uriName = new Uri(_url.Trim());

            try
            {
                string[] uriSegments = uriName.Segments;
                if (uriSegments.Length > 1)
                {
                    if (GenRandomNameImage(uriName.OriginalString) == string.Empty)
                        fileName = uriSegments[uriSegments.Length - 1];
                    else fileName = GenRandomNameImage(uriName.OriginalString);
                }
                else
                {
                    fileName = "default.html";
                }
          //      pFileServer = Global.Instance.LocalHost + @"/" + _cid.ToString() + @"/" + date + @"/"  + fileName.ToString().ToUpper();
         //       pFileServer =  @"/Images/" + fileName.ToString().ToUpper();
            // Modifield by VietLV
                pFileServer = fileName.ToString().ToUpper();

            }
            catch (Exception ex)
            {

                pFileServer = string.Empty;
            }

            return pFileServer;

        }
        public static string GenRandomNameImage(string url)
        {
            string[] ExtArray = { ".gif", ".jpg", ".bmp", ".jpeg", ".png", ".swf", ".flv", ".doc", ".zip", ".pdf" };

            foreach (string ext in ExtArray)
                if (url.ToLower().Trim().EndsWith(ext) == true)
                {
                    return string.Empty;
                }
            string sName = DateTime.Now.ToString("yyyMMddhhsstt") + ".jpg";
            return sName;

        }

       
    }
}
